package com.caterina.springBoard;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;

import com.caterina.activityManager.CActivity;
import com.caterina.components.COptionPane.MessageType;
import com.caterina.components.CPanel;
import com.caterina.resources.CWords;

import controllers.Permission;
import controllers.PermissionList;

public class Link {
	public Link(String tag,Permission...permissions){
		this.tag=tag;
		this.buttonPermissions=new PermissionList();
		for (Permission prm:permissions){this.buttonPermissions.add(prm);}
	}
	public Link(String tag,CActivity activity,Permission...permissions){
		this.tag=tag;
		this.activity=activity;
		this.buttonPermissions=new PermissionList();
		for (Permission prm:permissions){this.buttonPermissions.add(prm);}
	}
	public Link(String tag, AbstractOptionPane optionPane,Permission...permissions){
		this.tag=tag;
		this.optPane=optionPane;
		this.buttonPermissions=new PermissionList();
		for (Permission prm:permissions){this.buttonPermissions.add(prm);}
	}
	public void setParent(Container c){
		if(this.activity!=null)
		this.activity.setParent(c);
	}
	public boolean canView(PermissionList permissions){
		return buttonPermissions.can(permissions);
	}
	public CPanel getButton(final CActivity caller,PermissionList permissions){	
		if (buttonPermissions.can(permissions)){
			CPanel linkContainer=new CPanel(new BorderLayout());
			linkContainer.setOpaque(false);
			CSpringLink lnk= new CSpringLink(tag,caller) {
				@Override
				protected void onClick() {
					if (Link.this.activity!=null)this.setNextActivity(activity);
					else if(Link.this.optPane!=null)showPanel(caller);
					else{
						caller.showCOPtionPane(MessageType.WARNING_OK, "<html>"
							+"<h3>"+CWords.getWord("FUNCTIONALITY")+" "+CWords.getWord("UNAVABLE")+"</h3>"
							+CWords.getWord("FUNCTIONALITY")+": "
							+CWords.getWord(tag)+" "
							+CWords.getWord("UNAVABLE")+"</html>");
						this.inibite();
						}
				}
				private static final long serialVersionUID = 1L;
			};
			CPanel retPanel=new CPanel(new BorderLayout());
			retPanel.setOpaque(false);
			int w=130,h=18;
			retPanel.setMaximumSize(new Dimension(w,h));
			retPanel.setPreferredSize(new Dimension(w,h));
			retPanel.setMinimumSize(new Dimension(w,h));
			lnk.setOpaque(false);
			retPanel.add(lnk,BorderLayout.WEST);
			return retPanel;
		}else return null;
	}
	private void showPanel(CActivity caller){
		switch (optPane.getType()) {
		case ACTIVITY:{
			CActivity act=(CActivity)optPane.getContent();
			act.setParent(caller);
			caller.showCOPtionPane((CActivity) act);
			break;
		}
		default:
			break;
		}
	}
	private AbstractOptionPane optPane;
	private CActivity activity;
	private String tag;
	private PermissionList buttonPermissions;
}
